package com.swgk.core.view.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;

import com.swgk.core.R;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.databinding.LayoutBigHomeComparyBinding;
import com.swgk.core.util.ACAdminDialogUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.entity.EnterpriseEditEntity;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.swgk.core.view.weiget.CompanyMenuView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SimpleCardFragment extends AppLazyFragment {

    private LayoutBigHomeComparyBinding binding;
    public static final String KEY_TYPE = "type";
    private EnterpriseEditEntity menuEntity;
    private boolean iscontinue;
    public static MutableLiveData<Boolean> mutableLiveData;

    public static SimpleCardFragment getInstance(EnterpriseEditEntity data, MutableLiveData<Boolean> mutableLiveData1) {
        SimpleCardFragment fragment = new SimpleCardFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_TYPE, data);
        fragment.setArguments(bundle);
        mutableLiveData = mutableLiveData1;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.layout_big_home_compary, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        binding.companyMenu.setOnItemClick(new CompanyMenuView.OnItemClick(){

            @Override
            public void onIsUser(boolean isUser) {
                super.onIsUser(isUser);
                if (mutableLiveData != null && isVisible()){
                    mutableLiveData.postValue(isUser);
                }

                iscontinue = isUser;
            }
        });
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != menuEntity){
                    ActivityJump.startActivity(AppRouteConstant.EnterpriseConfigActivity, Pair.create("key",menuEntity.getKey()));
                }
            }
        });
    }

    @Override
    public void onLazyLoad() {
        if (getArguments().getSerializable(KEY_TYPE) == null) return;
        menuEntity = (EnterpriseEditEntity) getArguments().getSerializable(KEY_TYPE);
        if (menuEntity == null || menuEntity.getDtoList() == null) return;
        setBannerViewData(menuEntity.getDtoList());
    }

    /**
     * 切换下一页
     * @return true 切换成功；false 切换不成功
     */
    public boolean setNextTab(){
        if (binding != null && binding.companyMenu != null){
            boolean isLast = binding.companyMenu.selectTab();
            MLog.d("时间到 是否能切换下一页" + isLast);
            return isLast;
        }
       return false;
    }

    public void setNextTab(int index){
        if (binding != null && binding.companyMenu != null)
        binding.companyMenu.selectTab(index);
    }

    /**
     * 设置Banner的自定义view要显示的界面初始化
     *
     * @param enterpriseRecEntities
     */
    protected void setBannerViewData(List<EnterpriseRecEntity> enterpriseRecEntities) {
        List<EnterpriseRecEntity> enterpriseRecEntityList = new ArrayList<>();
        if (enterpriseRecEntities.size() > 18) {
            enterpriseRecEntityList.addAll(enterpriseRecEntities.subList(0, 18));
        } else {
            enterpriseRecEntityList.addAll(enterpriseRecEntities);
        }
        if (enterpriseRecEntityList.size() == 0) {
            binding.companyMenu.setVisibility(View.GONE);
            setDefaultView();
        } else {
            binding.linConfigDefault.setVisibility(View.GONE);
            binding.tvDefaultHint.setVisibility(View.GONE);
            binding.companyMenu.setVisibility(View.VISIBLE);
            binding.companyMenu.initData(getChildFragmentManager(), enterpriseRecEntityList);
        }
    }

    /**
     * 缺省页界面提示及按钮点击事件处理
     */
    public void setDefaultView() {
        LinearLayout linConfigDefault = binding.linConfigDefault;
        TextView tvDefaultHint = binding.tvDefaultHint;
        Button btnDefaultTxt = binding.btnDefaultTxt;
        Button btnDefault2Txt = binding.btnDefault2Txt;

        linConfigDefault.setVisibility(View.VISIBLE);
        btnDefaultTxt.setVisibility(View.VISIBLE);
        tvDefaultHint.setVisibility(View.VISIBLE);
        btnDefault2Txt.setVisibility(View.GONE);
        if (menuEntity == null || TextUtils.isEmpty(menuEntity.getName())) return;
        switch (menuEntity.getName()) {
            case "首页":
                tvDefaultHint.setText("前往配置首页企业");
                btnDefaultTxt.setText("企业配置");
                btnDefaultTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityJump.startActivity(AppRouteConstant.EnterpriseConfigActivity);
                        //showCompanyConfigWindow();
                    }
                });
                break;
            case "成员":
                tvDefaultHint.setText("您还没有成员企业，快去邀请吧！");
                btnDefaultTxt.setText("邀请成员");
                btnDefaultTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityJump.startActivity(AppRouteConstant.EnterpriseConfigActivity);
                    }
                });
                break;
            case "会员":
                tvDefaultHint.setText("您还没有会员企业，快去邀请会员吧！");
                btnDefaultTxt.setText("邀请会员");
                btnDefaultTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityJump.startActivity(AppRouteConstant.MemberInviteOrApplyActivity,
                                Pair.create("IS_MANAGE_PAGER",false),
                                Pair.create("CUR_PAGE",0),
                                Pair.create("INDEX",0)
                                );
                    }
                });
                btnDefault2Txt.setVisibility(View.VISIBLE);
                btnDefault2Txt.setText("加盟会员");
                btnDefault2Txt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityJump.startActivity(AppRouteConstant.MemberInviteOrApplyActivity,
                                Pair.create("IS_MANAGE_PAGER",false),
                                Pair.create("CUR_PAGE",1),
                                Pair.create("INDEX",0)
                        );
                    }
                });
                break;
            case "关注":
                tvDefaultHint.setText("您还没有关注其他企业，快去看看吧！");
                btnDefaultTxt.setText("企业大全");
                btnDefaultTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
                        String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");

                        ActivityJump.startActivity(AppRouteConstant.EnterpriseAndMapActivity,
                                Pair.create("isMap",false),
                                Pair.create("longitude",longitude),
                                Pair.create("latitude",latitude)
                        );
                    }
                });
                break;
            case "推荐":
                tvDefaultHint.setText("暂无推荐企业");
                btnDefaultTxt.setVisibility(View.GONE);
                break;
        }
    }

    private void showCompanyConfigWindow() {
        if (SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, "").equals("")) {
            EventBus.getDefault().postSticky("transport_to_login");
        } else {
            int TYPE = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
            if (TYPE == 0) {
                boolean isAuth = SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false);
                if (!isAuth) {
                    ACAdminDialogUtils.showACDialog(ACAdminDialogUtils.TYPE_AUTH_NO_NEW_AC, getContext());
                    return;
                }
            }
            ActivityJump.startActivity(AppRouteConstant.EnterpriseConfigActivity);
        }
    }

}
